<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title></title>
		<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/vant@1.6/lib/index.css">
		<link rel="stylesheet" type="text/css" href="../../css/register.css"/>
	</head>
	<body>
		<div id="app">
			<header >
				<van-row>
					<van-col span="2" class="centers black">  
						<van-icon @click="returns" name="arrow-left"></van-icon>
					</van-col>
				  	<van-col span="16" class="centers black">
				  		修改登录密码
				  	</van-col>
				  	<van-col span="6" class="centers black">
				  		<van-button type="danger" @click="revise">确认修改</van-button>
				  	</van-col>
				</van-row>
			</header>
			<section>
				<van-row class="padding">
				  	<van-col span="16" class="centers padding">  
				  		<van-cell-group>
				  			<van-field
							    v-model.lazy="verifi"
							    placeholder="6位数字短信验证码"
							    :error-message="verMsg"/>
				  		</van-cell-group>
					</van-col>
					<van-col span="8" class="centers">  
						<van-button type="danger" :disabled="dis" :text="val" @click="send"></van-button>
					</van-col>
					<van-col span="24" class="centers padding">  
						<van-cell-group>
							<van-field
								v-model.lazy="pwd"
								right-icon="eye-o"
								:type="password"
								placeholder="新的登录密码(最少6位)"
								:error-message="pwdMsg"
								@click-right-icon="shows"/>
						</van-cell-group>
					</van-col>
					<van-col span="24" class="centers padding">  
						<van-cell-group>
							<van-field
								v-model.lazy="pwds"
								right-icon="eye-o"
								:type="password1"
								placeholder="确认密码(再次输入登录密码)"
								:error-message="pwdsMsg"
								@click-right-icon="shows1"/>
						</van-cell-group>
					</van-col>
				</van-row>
			</section>
		</div>
		<script src="../../js/vue.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../js/vant.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../js/ajax.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			var app=new Vue({
				el:"#app",
				data:{
					password:"password",
					password1:"password",
					verMsg:"",
					pwdMsg:"",
					pwdsMsg:"",
					verifi:"",
					pwd:"",
					pwds:"",
					phone:"",
					val:"获取验证码",
					dis:false,
				},
				methods:{
					shows:function(){
						this.password = this.password === 'password' ? 'text' : 'password';
					},
					shows1:function(){
						this.password1 = this.password1 === 'password' ? 'text' : 'password';
					},
					returns:function(){
						plus.webview.close(plus.webview.currentWebview(), "slide-out-left");
					},
					send:function(){
						var _this = this;
 						var newUser = localStorage.getItem("user"); //从localStorage里取数据(字符串格式)
 						newUser = JSON.parse(newUser); //把字符串格式转换成Json格式
 						if(newUser!=null){
 							ajax({
							    url:"http://dsapi.ysd3g.com/API/SendChangePasswordCode",
							    dataType:"jsonp",
							    data:{
							        key:newUser.Key
							    },
							    success(res){
							        console.log(JSON.stringify(res));
							        if (res.Error) {
							            app.$toast.fail(res.Msg);
							        } else{
							            _this.$dialog.confirm({
							            	title: '验证码',
							            	message: res.Code,
							            	confirmButtonText: '复制'
							            }).then(() => {
							            	//复制
							            	app.verifi=res.Code;
											_this.getSecond(60);
							            }).catch(() => {
							            	//取消
											_this.getSecond(60);
							            });
							        }
							    }
							});
 						}else{
							_this.$dialog.confirm({
								title: '您还未登录，是否登录？'
							}).then(() => {
								//确认按钮
					            plus.webview.close(plus.webview.currentWebview(),"slide-out-left");//关闭当前页面,返回个人中心
				            	plus.webview.create("login.html","login").show("slide-out-left");
							}).catch(() => {
								//取消按钮
								
							});
						}
					},
					getSecond:function(time){
						let _this=this;
						let  _time= time;
						if (_time==0) {
							_this.dis=false;
							_this.val="获取验证码";
							time=_time;
						}else{
							_this.dis=true;
							_this.val=time+"s 后重试";
							time--;
							setTimeout(() => {
								_this.getSecond(time);
							}, 1000)
						}
					},
					revise:function(){
						var _this = this;
 						var newUser = localStorage.getItem("user"); //从localStorage里取数据(字符串格式)
 						console.log("nihao");
 						newUser = JSON.parse(newUser); //把字符串格式转换成Json格式
						ajax({
						    url: "http://dsapi.ysd3g.com/API/ChangePassword",
						    dataType: "jsonp",
						    data: {
						        key:newUser.Key,
						        password: app.pwd,
						        phoneCode:app.verifi
						    },
						    success(res) {
						    	console.log(JSON.stringify(res));
						        if (res.Error) {
						            app.$toast.fail(res.Msg);
						        } else{
						            _this.$dialog.confirm({
										title: '密码已修改，请重新登录'
									}).then(() => {
										//确认按钮
							            localStorage.clear();//清除用户信息
										var presonal=plus.webview.getWebviewById("presonal");
										presonal.reload(true);
										plus.webview.close(plus.webview.currentWebview(),"slide-out-left");
							            plus.webview.create("login.html","login").show("slide-out-left");
									}).catch(() => {
										//取消按钮
										localStorage.clear();//清除用户信息
										var presonal=plus.webview.getWebviewById("presonal");
										presonal.reload(true);
										plus.webview.close(plus.webview.currentWebview(),"slide-out-left");
									});
						        }
						    }
						})
					}
				},
				watch:{
					verifi(newVer, oldVer) {
  						if(!(/^\d{6}$/.test(newVer))){
  							this.verMsg="验证码错误";
  						}else{
  							this.verMsg="";
  						}
					},
					pwd(newPwd, oldPwd) {
  						if(!(/^(\w){6,16}$/.test(newPwd))){
  							this.pwdMsg="密码必须在6-16位之间";
  						}else{
  							this.pwdMsg="";
  						}
					},
					pwds(newPwds, oldPwds) {
  						if(newPwds!=this.pwd){
  							this.pwdsMsg="两次密码不一致";
  						}else{
  							this.pwdsMsg="";
  						}
					},
  				}
			});
		</script>
	</body>
</html>